package com.ruoyi.system.service.vantservice.orderservice.impl;

import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderVoucher;
import com.ruoyi.system.mapper.vant.order.ApiOrderVoucherMapper;
import com.ruoyi.system.service.vantservice.orderservice.ApiOrderVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class ApiOrderVoucherServiceImpl implements ApiOrderVoucherService {

    @Autowired
    private ApiOrderVoucherMapper apiOrderVoucherMapper;

    /**
     * 添加订单凭证信息
     * @param orderVoucher
     * @return
     */
    public int insertOrderVoucher(OrderVoucher orderVoucher){
        return apiOrderVoucherMapper.insertOrderVoucher(orderVoucher);
    }

    @Override
    public Map<String, Object> uploadPingzheng(MultipartFile[] files) {
        Map<String, Object> map = new HashMap<>();
//        String url = "D:\\Java\\xiazai\\";
        String url = "/home/ruoyi/profile/xiazai";
        String fileName = null;
        String fileNameOne = "tu";
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                try {
                    //上传地址
                    File floader = new File(url);
                    if (!floader.exists()){
                        floader.mkdir();//如果不存在这个文件夹,就创建上
                    }
                    //获取文件名
                    fileName = files[i].getOriginalFilename();
//                    fileName = "tupian";
                    System.err.println("************fileName***********fileName***********"+fileName);
                    //文件输出流
                    FileOutputStream fileOutputStream = null;
                    String ns = fileName.substring(0, fileName.indexOf("."));//获取名字.之前的
                    String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
                    String newFileNamePrefix = UUID.randomUUID().toString().replace("-", "") + new SimpleDateFormat("yyyyMMddHHssSSS").format(new Date());
                    String newFileName = newFileNamePrefix + suffix;
                    fileOutputStream = new FileOutputStream(url + newFileName);
                    //包装流
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                    //写入
                    bufferedOutputStream.write(files[i].getBytes());
                    //缓冲 关闭流
                    bufferedOutputStream.flush();
                    bufferedOutputStream.close();
//                    map.put("img"+String.valueOf(i),newFileName);//images/232132eadsa32332432.jpg
                    map.put(fileNameOne,newFileName);//images/232132eadsa32332432.jpg
                    map.put("code",200);
                } catch (Exception e) {
                    e.printStackTrace();
                    map.put("code",500);
                    return map;
                }
            }
        }
        return map;
    }
}
